﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Helpers;

namespace HtmlParsing
{
    public class UrlParser
    {
        public static UrlInfo Parse(string url)
        {
            UrlInfo result = new UrlInfo();
            result.QueryArgs = new Dictionary<string, List<string>>();

            string fullUrl = url.ToLower();

            if (fullUrl.Contains(":"))
            {
                string[] urlParts = fullUrl.Split(':');
                if (urlParts.Length != 2)
                    throw new Exception("Wrong URL format");

                result.Protocol = urlParts[0].Trim();

                fullUrl = urlParts[1].Trim();
            }

            if (fullUrl.StartsWith("/"))
                fullUrl = fullUrl.Substring(1);

            if (fullUrl.StartsWith("/"))
                fullUrl = fullUrl.Substring(1);

            if (fullUrl.StartsWith("/"))
                throw new Exception("Wrong URL format");

            string address = null;
            string qs = null;
            if (fullUrl.Contains("?"))
            {
                string[] addressParts = fullUrl.Split('?');
                if (addressParts.Length == 1)
                {
                    address = addressParts[0].Trim();
                }
                else if (addressParts.Length == 2)
                {
                    address = addressParts[0].Trim();
                    qs = addressParts[1].Trim();
                }
                else
                    throw new Exception("Wrong URL format");
            }
            else
            {
                address = fullUrl;
            }

            if (!StringHelper.IsnullOrBlank(address))
            {
                if (address.Contains("/"))
                {
                    string[] addressParts = address.Split('/');
                    if (addressParts.Length == 1)
                    {
                        result.SiteAddress = addressParts[0].Trim();
                        result.RelativePath = string.Empty;
                    }
                    else if (addressParts.Length > 1)
                    {
                        result.SiteAddress = addressParts[0].Trim();
                        result.RelativePath = string.Join("/", addressParts.Skip(1).ToArray());
                    }
                    else
                        throw new Exception("Wrong URL format");
                }
                else
                {
                    result.SiteAddress = address;
                    result.RelativePath = null;
                }
            }

            if (!StringHelper.IsnullOrBlank(qs))
            {
                string[] qsParts = null;
                if (qs.Contains("&"))
                    qsParts = qs.Split('&');
                else
                    qsParts = new string[] { qs };

                qsParts.ToList().ForEach(qsPart =>
                    {
                        if (StringHelper.IsnullOrBlank(qsPart))
                            return;
                        if(!qsPart.Contains("="))
                            throw new Exception("Wrong URL format");

                        string[] argParts = qsPart.Split('=');
                        if(argParts.Length != 2)
                            throw new Exception("Wrong URL format");

                        string argName = argParts[0].Trim();
                        string argValue = argParts[1].Trim();

                        if (!result.QueryArgs.ContainsKey(argName))
                        {
                            result.QueryArgs.Add(argName, new List<string>());
                        }

                        if (!result.QueryArgs[argName].Contains(argValue))
                            result.QueryArgs[argName].Add(argValue);
                    });
            }


            return result;
        }
    }

    public class UrlInfo
    {
        public string Protocol { get; set; }
        public string SiteAddress { get; set; }
        public string RelativePath { get; set; }
        public Dictionary<string, List<string>> QueryArgs { get; set; }
    }
}
